package com.wjjun.basemvpvm.data.request;

import android.text.TextUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * 基础的请求接口
 * author Wjjun
 * Created 2018/3/20.
 */

public class BaseRequest {

    private String Token;

    public String getToken() {
        return Token;
    }

    public void setToken(String token) {
        this.Token = token;
    }


    public Map<String, String> toMap() {
        return objectToMap(this);
    }

    public Map<String, String> objectToMap(Object obj) {
        if (obj == null) {
            return null;
        }

        Map<String, String> map = new HashMap<>();

        Field[] declaredFields = obj.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            try {
                if ("serialVersionUID".equals(field.getName())) {
                    continue;
                }
                Object value = field.get(obj);
                if (value == null) {
                    continue;
                }
                if (value instanceof String && TextUtils.isEmpty(((String) value))) {
                    continue;
                }
                map.put(field.getName(), value.toString());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return map;
    }
}
